package com.shire.shire_interface.service.massmsg;

import com.shire.shire_interface.dao.massmsg.WxMassMsgItemDao;
import com.shire.shire_interface.dao.massmsg.WxMassMsgTaskDao;
import com.shire.shire_interface.model.massmsg.WxMassMsgItem;
import com.shire.shire_interface.model.massmsg.WxMassMsgItemExample;
import com.shire.shire_interface.model.massmsg.WxMassMsgTask;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者:  sll
 * 时间:  2019/8/13 16:04
 */
@Service
public class WxMassMsgItemService {

    @Resource
    private WxMassMsgItemDao wxMassMsgItemDao;

    @Resource
    private WxMassMsgTaskDao msgTaskDao;

    public Integer insert(WxMassMsgItem wxMassMsgItem) {
        return wxMassMsgItemDao.insertSelective(wxMassMsgItem);
    }


    public int updateItem(Integer id, Integer rstStatus){
        WxMassMsgItem item = wxMassMsgItemDao.selectByPrimaryKey(id);
        if (item == null )
            return  -1;
        item.setRststatus(rstStatus);

        WxMassMsgTask task = msgTaskDao.selectByPrimaryKey(item.getTaskid());
        if (task != null && rstStatus != 0){
            if (task.getFailcount() == null){
                task.setFailcount(0);
            }else {
                task.setFailcount(task.getFailcount() + 1);
            }
        }
        msgTaskDao.updateByPrimaryKey(task);
        return wxMassMsgItemDao.updateByPrimaryKey(item);
    }

    public  List<WxMassMsgItem> selectByExample(WxMassMsgItemExample wxMassMsgItemExample) {

        return wxMassMsgItemDao.selectByExample(wxMassMsgItemExample);
    }
}
